package com.cos.blog.controller.web;

import com.alibaba.fastjson.JSON;
import com.cos.blog.core.constants.BaseConstants;
import com.cos.blog.core.servlet.BaseServlet;
import com.cos.blog.core.utils.EmailUtils;
import com.cos.blog.dto.EmailDto;
import com.cos.blog.entity.UserEntity;
import com.cos.blog.services.IUserService;
import com.cos.blog.services.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;

/**
 * @Author:zxc
 * @Description: 登录注册
 */
@WebServlet("/UserServlet/*")
public class UserServlet extends BaseServlet {

    private IUserService userService = new UserServiceImpl();

    /**
     * @Author:zxc
     * @Description: 注册
     */
    public String register() throws Exception {
        String s = req.getReader().readLine();
        UserEntity userEntity = JSON.parseObject(s, UserEntity.class);

        // 1.判断当前邮箱或者用户名是否已经被注册了(去数据库根据用户名或者邮箱查询一下)

        // 2.调用业务层的注册方法
        userService.register(userEntity);
        String activationCode = userEntity.getActivationCode();
        // 3.给用户的邮箱发送激活账号的连接
        EmailDto emailDto = new EmailDto();
        emailDto.setTilte("Blog新用户注册账号激活");
        emailDto.setToUser(userEntity.getEmail());
        emailDto.setContent("<a href = 'http://2207com:8080/UserServlet/activation?code=" + activationCode + "'>点击这里激活</a>");
//        emailDto.setContent("<a href ='http://127.0.0.1:8080/IndexServlet/index'>点击这里激活</a>");
        EmailUtils.sendEmailInfoUser(emailDto);

        // 4.提示用户
        return outJson(true);
    }

    // 激活哪个账号？
    public String activation() throws Exception {
        // 1.获取激活码
        String activationCode = req.getParameter("code");
        System.out.println("激活码:" + activationCode);
        // 2.验证
        UserEntity userEntity = userService.byActivationCode(activationCode);
        if (userEntity == null) {
            System.out.println("激活码有误");
            return BaseConstants.BASE_SCRIPT + ":激活码有误";
        } else {
            userEntity.setStatus(2);
            userService.update(userEntity);
            return BaseConstants.BASE_REDIRECT + ":PageServlet/view/site/login.html";
        }
    }

    public String login() throws Exception {
        //getReader 获取json类型的数据
        String s = req.getReader().readLine();
        UserEntity userEntity = JSON.parseObject(s, UserEntity.class);
        //比对用户名和密码  数据库密码需要加密
        userEntity = userService.login(userEntity.getUsername(), userEntity.getPassword());
        // 判断认证成功
        if (userEntity == null) {
            return outJson(100, "用户名获密码错误1");
        }
        // 登录成功
        userEntity.setPassword(null); // 把密码设置为null，然哎放到session中
        req.getSession().setAttribute("loginUser", userEntity);
        System.out.println("333333");
        return outJson(200, "登录成功");
    }

//    public String updapwd() throws Exception {
//        //getReader 获取json类型的数据
//        String s = req.getReader().readLine();
//        UserEntity userEntity = JSON.parseObject(s, UserEntity.class);
//        //比对用户名和密码  数据库密码需要加密
//        userEntity = userService.login(userEntity.getUsername(), userEntity.getPassword());
//        // 判断认证成功
//        if (userEntity == null) {
//            return outJson(100, "用户名获密码错误");
//        }
//
//        return outJson(200,"修改成功");
//    }

    /**
     * @Author:zxc
     * @Description: 注销
     */
    public void logout() throws Exception {
        //注销，失效
        req.getSession().invalidate();
//        req.getSession().removeAttribute("loginUser");
        // 注销成功后跳转到登录页面
//        return BaseConstants.BASE_REDIRECT+":/pageServlet/xxxxx"
        outView("view/site/login", null);
    }
}
